package com.example.teach.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teach.entity.Schedule;
import com.example.teach.mapper.ScheduleMapper;
import com.example.teach.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements ScheduleService {

    @Autowired
    private ScheduleMapper scheduleMapper;

    @Override
    public String create(Schedule schedule) {
        //LambdaQueryWrapper<Class> c = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Schedule> wrapper = new LambdaQueryWrapper<>();
        for(Schedule existingSchedule : scheduleMapper.selectList(wrapper)) {
            if((schedule.getStartWeek() < existingSchedule.getEndWeek())
                    && (schedule.getEndWeek() > existingSchedule.getStartWeek())) {
                if(schedule.getDay().equals(existingSchedule.getDay())) {
                    if((schedule.getStartTime() < existingSchedule.getEndTime()
                            && schedule.getEndTime() > existingSchedule.getStartTime())){
                        if(schedule.getClassroom().equals(existingSchedule.getClassroom())) {
                            return "地点有冲突";
                        }else{
                            return "时间有冲突";
                        }
                    }
                }
            }
        }
        scheduleMapper.insert(schedule);
        return "课程创建成功";
    }

    @Override
    public void deleteById(Integer classId) {
        scheduleMapper.deleteByclassId(classId);
    }
}
